﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class KLADR : IEquatable<KLADR>
    {
        public void ImportList(List<Classes.KLADR> KLADRList)
        {
            var mdb = new MedregDBDataContext();
            var ListKLADR = new List<KLADR>();
            foreach (var KLADR in KLADRList)
            {
                KLADR KLADR1 = new KLADR() { ID = KLADR.ID, Name = KLADR.Name, Prefix = KLADR.Prefix, Parent = KLADR.Parent};
                KLADR query;

                if (mdb.KLADR.LongCount() == 0)
                {
                    mdb.KLADR.InsertOnSubmit(KLADR1);
                }
                else
                {
                    try
                    {
                        query = mdb.KLADR.Single(l => l.ID == KLADR1.ID);
                        query.Name = KLADR1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListKLADR.Contains(KLADR1))
                        {
                            mdb.KLADR.InsertOnSubmit(KLADR1);
                            ListKLADR.Add(KLADR1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(KLADR other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
